package com.dries.portal.module.convener.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dries.common.api.CommonResult;
import com.dries.common.exception.ApiException;
import com.dries.portal.module.convener.entity.JoinConvener;
import com.dries.portal.module.convener.enums.JoinConvenerType;
import com.dries.portal.module.convener.service.IJoinConvenerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 召集令预报名
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-09-11 16:58:44
 */
@RestController
@RequestMapping("convener/join")
public class JoinConvenerController {

    @Resource
    private IJoinConvenerService joinConvenerService;
    /**
     * 列表
     */
    @GetMapping("/list")
    public CommonResult list(){
        return CommonResult.success();
    }
    /**
     * 当前召集令预报名列表
     */
    @GetMapping("/info/{id}")
    public CommonResult info(@PathVariable("id") Integer id){
        List<JoinConvener> list = joinConvenerService.list(new QueryWrapper<JoinConvener>().lambda()
                .eq(JoinConvener::getConvenerId, id)
                .eq(JoinConvener::getStatus, JoinConvenerType.NO_SIGNUP.getCode())
                .orderByDesc(JoinConvener::getCreateTime)
        );
        return CommonResult.success(list);
    }
    /**
     * 信息
     */
    @ApiOperation("召集令已确认报名列表")
    @GetMapping("/users/{id}")
    public CommonResult users(@PathVariable("id") Integer id) {

        List<JoinConvener> list = joinConvenerService.list(new QueryWrapper<JoinConvener>().lambda()
                .eq(JoinConvener::getConvenerId, id)
                .eq(JoinConvener::getStatus, JoinConvenerType.SIGNUP.getCode())
                .orderByDesc(JoinConvener::getCreateTime)
        );
        return CommonResult.success(list);
    }
    /**
     * 信息
     */
    @ApiOperation("召集令取消报名")
    @GetMapping("/cancel/{id}/{userId}")
    public CommonResult cancel(@PathVariable("id") Integer id, @PathVariable("userId") String userId) {
        boolean result = joinConvenerService.update(new UpdateWrapper<JoinConvener>().lambda()
                .set(JoinConvener::getStatus, JoinConvenerType.NO_SIGNUP.getCode())
                .eq(JoinConvener::getConvenerId, id)
                .eq(JoinConvener::getUserId, userId)
        );

        return CommonResult.success(result);
    }
    /**
     * 信息
     */
    @ApiOperation("召集令已取消报名列表")
    @GetMapping("/cancels/{id}")
    public CommonResult cancels(@PathVariable("id") Integer id) {

        List<JoinConvener> list = joinConvenerService.list(new QueryWrapper<JoinConvener>().lambda()
                .eq(JoinConvener::getConvenerId, id)
                .eq(JoinConvener::getStatus, JoinConvenerType.NO_JOIN.getCode())
                .orderByDesc(JoinConvener::getCreateTime)
        );
        return CommonResult.success(list);
    }

    /**
     * 预报名申请
     */
    @PostMapping("/save")
    public CommonResult save(@RequestBody JoinConvener joinConvener){

        JoinConvener convener = joinConvenerService.getOne(new QueryWrapper<JoinConvener>().lambda()
                .eq(JoinConvener::getConvenerId, joinConvener.getId())
                .eq(JoinConvener::getUserId, joinConvener.getUserId())
        );

        if (Objects.nonNull(convener)) {
            throw new ApiException("您已成功报名，请等待审核");
        }

        joinConvenerService.save(joinConvener);
        return CommonResult.success("预报名成功");
    }

    /**
     * 是否已经报名
     */
    @GetMapping("/check/{id}/{userId}")
    public CommonResult apply(@PathVariable String id, @PathVariable String userId){
        JoinConvener convener = joinConvenerService.getOne(new QueryWrapper<JoinConvener>().lambda()
                .eq(JoinConvener::getConvenerId, id)
                .eq(JoinConvener::getUserId, userId)
        );
        if (convener != null) {
            return CommonResult.success(1);
        }
        return CommonResult.success(0);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody JoinConvener joinConvener){
		joinConvenerService.updateById(joinConvener);
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] ids){
		joinConvenerService.removeByIds(Arrays.asList(ids));

        return CommonResult.success();
    }

}
